PIC := APIC
CFLAGS = -mcmodel=large -fno-builtin -m64 -c -D$(PIC)
ASFLAGS = --64

SYSTEM_OBJS = head.o entry.o main.o printk.o trap.o gate.o lib.o memory.o interrupt.o task.o cpu.o pic.o keyboard.o mouse.o disk.o

all: system
	objcopy -I elf64-x86-64 -S -R ".eh_frame" -R ".comment" -O binary system kernel.bin

system:$(SYSTEM_OBJS)
	ld -b elf64-x86-64 -o system $^ -T Kernel.lds

head.o:head.S
	gcc -E head.S > head.s
	as $(ASFLAGS) -o head.o head.s

entry.o:entry.S
	gcc -E entry.S > entry.s
	as $(ASFLAGS) -o entry.o entry.s

%.o:%.c
	gcc $(CFLAGS) $<

ifeq ($(PIC), APIC)
pic.o: apic.c
	gcc $(CFLAGS) apic.c -o pic.o
else
pic.o: 8259a.c
	gcc $(CFLAGS) 8259a.c -o pic.o
endif

clean:
	rm -rf *.o *~ *.s system kernel.bin
